package cc.vv.btpartyjob.partymanager.activity

import android.content.Intent
import android.support.v4.app.Fragment
import android.support.v4.view.ViewPager
import android.view.View
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.jnimodule.activity.QuanXainOrganizationActivity
import cc.vv.btpartyjob.jnimodule.globle.IntentJniKey
import cc.vv.btpartyjob.jnimodule.server.JniStartActivity
import cc.vv.btpartyjob.jnimodule.server.UserRoleServer
import cc.vv.btpartyjob.partymanager.R
import cc.vv.btpartyjob.partymanager.adapter.ManagerViewPageAdapter
import cc.vv.btpartyjob.partymanager.delegate.ManagerBaseBinder
import cc.vv.btpartyjob.partymanager.delegate.UnitPromiseActivityDelegate
import cc.vv.btpartyjob.partymanager.fragment.EvaluateNoFragment
import cc.vv.btpartyjob.partymanager.fragment.EvaluateYesFragment
import cc.vv.btpartyjob.partymanager.fragment.UnitEvaluateNoFragment
import cc.vv.btpartyjob.partymanager.fragment.UnitEvaluateYesFragment
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder

/**
 * 单位承诺
 * Created by Chen.qi on 2018/10/29.
 */
class UnitPromiseActivity : BaseActivityMVP<UnitPromiseActivityDelegate>() {

    /**
     * ViewPager适配器
     */
    private var mAdapter: ManagerViewPageAdapter? = null

    /**
     * Fragment集合
     */
    private var mFragments: ArrayList<Fragment>? = null

    override fun initData() {
        mFragments = ArrayList()
        val registerNoFragment = UnitEvaluateNoFragment()
        val registerYesFragment = UnitEvaluateYesFragment()
        mFragments!!.add(registerNoFragment)
        mFragments!!.add(registerYesFragment)
        mAdapter = ManagerViewPageAdapter(supportFragmentManager, mFragments!!)
        viewDelegate?.setAdapterData(mAdapter!!)
        viewDelegate?.setEvaluateSelect(true)

        if (UserRoleServer.getAreaRole() || UserRoleServer.getStreetRole()
                || UserRoleServer.getSheQuRole() || UserRoleServer.getGridRole()) {
            viewDelegate?.setTopBarTitle("单位承诺", true)
        } else {
            viewDelegate?.setTopBarTitle("单位承诺", false)
        }

        if (UserRoleServer.getUnitMangerWeiRole()) {
            viewDelegate?.showUnitPromise(true)
        } else {
            viewDelegate?.showUnitPromise(false)

        }
    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {}
            override fun rightOnClick() {
                val intent = Intent(this@UnitPromiseActivity, QuanXainOrganizationActivity::class.java)
                startActivityForResult(intent, 100)
            }
        })

        viewDelegate?.setOnClickListener(View.OnClickListener {
            //未评价
            viewDelegate?.setEvaluateSelect(true)
            viewDelegate?.setViewPagerPos(0)
        }, R.id.tv_app_evaluateNo)


        viewDelegate?.setOnClickListener(View.OnClickListener {
            //已评价
            viewDelegate?.setEvaluateSelect(false)
            viewDelegate?.setViewPagerPos(1)
        }, R.id.tv_app_evaluateYes)

        viewDelegate?.bindViewPagerLisener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                if (position == 0) {
                    viewDelegate?.setEvaluateSelect(true)
                } else {
                    viewDelegate?.setEvaluateSelect(false)
                }
            }
        })

        viewDelegate.setOnClickListener(View.OnClickListener {
            //添加承诺
            val intent = Intent(this, AddPromiseActivity::class.java)
            intent.putExtra("type", 1)
            startActivity(intent)
        }, R.id.iv_unit_floating)

    }

    override fun getDelegateClass(): Class<UnitPromiseActivityDelegate> {
        return UnitPromiseActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return ManagerBaseBinder()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 100 && resultCode == 101) {
            val communityId = data?.getStringExtra(IntentJniKey.INTENT_KEY_ORGAN_ID)
            try {
                val noFrag = mFragments!![0] as UnitEvaluateNoFragment
                noFrag.unitCommitNoHttp(communityId)
                val yesFrag = mFragments!![1] as UnitEvaluateYesFragment
                yesFrag.unitCommitYesHttp(communityId)
            } catch (e: Exception) {

            }
        }
    }

}